﻿using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Practices.ServiceLocation;
using PCManagerReloaded.Model;

namespace PCManagerReloaded.ViewModel
{
    public class DownloadsViewModel : ViewModelBase
    {
        public DownloadsViewModel()
        {
            Items = new ObservableCollection<FileItemViewModel>();

            if (IsInDesignMode)
            {
                Items.Add(new FileItemViewModel() { FileName = "Test" });
                Items.Add(new FileItemViewModel() { FileName = "Test" }); Items.Add(new FileItemViewModel() { FileName = "Test" }); Items.Add(new FileItemViewModel() { FileName = "Test" }); Items.Add(new FileItemViewModel() { FileName = "Test" }); Items.Add(new FileItemViewModel() { FileName = "Test" }); Items.Add(new FileItemViewModel() { FileName = "Test" }); Items.Add(new FileItemViewModel() { FileName = "Test" }); Items.Add(new FileItemViewModel() { FileName = "Test" }); Items.Add(new FileItemViewModel() { FileName = "Test" }); Items.Add(new FileItemViewModel() { FileName = "Test" }); Items.Add(new FileItemViewModel() { FileName = "Test" }); Items.Add(new FileItemViewModel() { FileName = "Test" });
            }
            else
            {
                this.Refresh();
            }
            
        }

        public string Title { get { return "PC Manager Reloaded"; } }
        public string PageName { get { return "DOWNLOADS"; } }

        private string _downloadNotificationMessage;
        private ObservableCollection<FileItemViewModel> _items;
        private bool _isBusy;

        public string DownloadNotificationMessage
        {
            get { return _downloadNotificationMessage; }
            set
            {
                _downloadNotificationMessage = value;
                RaisePropertyChanged(()=>DownloadNotificationMessage);
            }
        }

        public ObservableCollection<FileItemViewModel> Items
        {
            get { return _items; }
            set
            {
                if (_items != null)
                    _items.CollectionChanged -= new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_items_CollectionChanged);
                _items = value;
                if (_items != null)
                    _items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_items_CollectionChanged );
                RaisePropertyChanged(()=>Items);
            }
        }

        void _items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            DownloadNotificationMessage = string.Format("new ({0})", _items.Count);
        }

        public void Refresh()
        {
            this.IsBusy = true;

            var modelProvider = ServiceLocator.Current.GetInstance<FileModelProvider>();

            try
            {
                modelProvider.LoadFilesAsync(args =>
                                                 {
                                                     IsBusy = false;

                                                         if (args.Error != null)
                                                         {
                                                              CustomMessageBox messageBox = new CustomMessageBox()
                                                                {                
                                                                    Caption = "Exception occurred",
                                                                    Message = args.Error.Message,
                                                                    LeftButtonContent = "Try again",
                                                                    RightButtonContent = "Cancel",
                                                                    IsFullScreen = true
                                                                };
                                                              messageBox.Dismissed += (s1, e1) =>
                                                              {
                                                                  switch (e1.Result)
                                                                  {
                                                                      case CustomMessageBoxResult.LeftButton:
                                                                          // Do something.
                                                                          Refresh();
                                                                          break;
                                                                      case CustomMessageBoxResult.RightButton:
                                                                          // Do something.
                                                                          break;
                                                                      case CustomMessageBoxResult.None:
                                                                          // Do something.
                                                                          break;
                                                                      default:
                                                                          break;
                                                                  }
                                                              };

                                                              messageBox.Show();

                                                         }

                                                         this.Items.Clear();

                                                         foreach (var item in args.Result)
                                                         {
                                                             Items.Add(new FileItemViewModel() { FileName = item.Name });
                                                         }

                                                         this.IsBusy = false;
                                                     });
            }
            catch (Exception)
            {
            }
        }

        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                RaisePropertyChanged(()=>IsBusy);
                RaisePropertyChanged(() => IsNotBusy);
            }
        }

        public bool IsNotBusy
        {
            get { return !_isBusy; }
            set { _isBusy = !value; }
        }
    }
}
